package com.example.administrator.hookdemo;

import android.app.Activity;
import android.app.ActivityThread;
import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        replaceContextInstrumentation(getBaseContext());
        Intent intent = new Intent(this,Main2Activity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getBaseContext().startActivity(intent);
//        A a = new B();
//        try {
//            Field field = B.class.getDeclaredField("name");
//            field.setAccessible(true);
//            field.set(a,"456");
//        } catch (NoSuchFieldException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }

//        Log.e ("androidLog",((B)a).getName());

    }

    interface A{
    }

    class B implements A{
        private String name="123";

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }



    public void replaceContextInstrumentation(Context context){

        try {
            Class contextImpl = Class.forName("android.app.ActivityThread");
            Field field = contextImpl.getDeclaredField("sCurrentActivityThread");
            field.setAccessible(true);
            ActivityThread activityThread = (ActivityThread) field.get(null);

            Field field1 = ActivityThread.class.getDeclaredField("mInstrumentation");
            field1.setAccessible(true);
            Instrumentation instrumentation = (Instrumentation) field1.get(activityThread);
            InstrumentationProxy instrumentationProxy = new InstrumentationProxy(instrumentation);
            field1.set(activityThread,instrumentationProxy);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
            Log.e("androidLog",e.getMessage()+"===");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            Log.e("androidLog",e.getMessage()+"---");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void replaceActivityInstrumentation(Activity activity){
        try {
            Field field = Activity.class.getDeclaredField("mInstrumentation");
            field.setAccessible(true);
            Instrumentation instrumentation = (Instrumentation) field.get(activity);
            InstrumentationProxy instrumentationProxy = new InstrumentationProxy(instrumentation);
            field.set(activity,instrumentationProxy);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }


}
